package com.ruoyi.http.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.date.DateFormater;
import com.ruoyi.http.domain.Http;
import com.ruoyi.http.domain.WeChat00300T;
import com.ruoyi.http.domain.WeChat00800T;
import com.ruoyi.http.domain.WeChat00900T;
import com.ruoyi.http.service.IWeChatTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/http/weChat00900")
public class WeChat00900 extends BaseController {

    @Autowired
    private IWeChatTService weChatTService;

    @Resource
    private AuthenticationManager authenticationManager;
    @GetMapping("/weChat")
    public AjaxResult http(WeChat00900T weChat00900T) {
        List<WeChat00900T> weChat = new ArrayList<WeChat00900T>();
        WeChat00900T weChatVO = new WeChat00900T();
        if(weChat00900T.getUsercode()==null||weChat00900T.getPassword()==null) {
            String msg = "账号或者密码不能为空";
            return AjaxResult.error(msg);
        }
        Authentication authentication = null;
        try
        {
            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
            authentication = authenticationManager
                    .authenticate(new UsernamePasswordAuthenticationToken(weChat00900T.getUsercode(), weChat00900T.getPassword()));
        }catch (Exception e)
        {
            String msg = "账号或者密码错误";
            return AjaxResult.error(msg);
        }
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        weChat00900T.setPassword(loginUser.getUser().getPassword());
        weChatVO = weChatTService.getCompanyInfo(weChat00900T);
        weChat = weChatTService.getProjectInfoByTime(weChat00900T);
        return AjaxResult.success(weChat);
    }
}